package jvm.jtacck.db.records;

/**
 * @author Chris Gamble
 * 
 *         Represents a User record in the database. A user has a username, a
 *         password, a first and last name, as well as the client_site he/she is
 *         associated with and the type of user they are.
 */
public class User extends Record {

    private static final long serialVersionUID = 6496590428267700346L;
    public final String username;
    public final String password;
    public final String fname;
    public final String lname;
    public final ClientSite site;
    public final UserType type;

    /**
     * Constructs a new User
     * 
     * @param username
     *            The primary key of a User(s)
     *            NON-NULLABLE
     * @param password
     *            The encrypted password of the user
     *            NON-NULLABLE
     * @param fname
     *            The first name of the user
     * @param lname
     *            The last name of the user
     * @param siteID
     *            The client_id of the Client_Site this user is associated with
     *            NON-NULLABLE
     * @param typeID
     *            The type_id of the User_Type that this user is
     *            NON-NULLABLE
     */
    public User(String username, String password, String fname, String lname,
            ClientSite site, UserType type) {
        super();
        this.username = username;
        this.password = password;
        this.fname = fname;
        this.lname = lname;
        this.site = site;
        this.type = type;
    }
    
    @Override
    public boolean equals(Object o){
        if (o instanceof User) {
            User other = (User) o;
            boolean x =
                   username.equals(other.username) &&
                   password.equals(other.password) &&
                   checkString(fname, other.fname) &&
                   checkString(lname, other.lname) &&
                   site.equals(other.site) &&
                   type.equals(other.type);
            return x;
        }
        return false;
    }
    
    public boolean checkString(String s1, String s2) {
        if (s1 == null) {
            return s2 == null;
        }else {
            return s1.equals(s2);
        }
    }
    
    @Override
    public int hashCode() {
        return username.hashCode() + fname.hashCode() + 
        lname.hashCode() + site.hashCode() + type.hashCode();
    }
    
    public String toString() {
        return username + ": " + fname + ", " + lname + ", " + 
                site.name + ", " + type.name + ".";
    }
}